function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return decodeURIComponent(r[2]);
  }
  return null;
}
if (window.location.host.indexOf("192.168") > -1) {
   // 用户状态是否成交30天
   var GetClientContractStatus ='//web.tcnwc.weitutorstage.com/mgmplatform/MgmApi/GetClientContractStatus';
  // 获取用户信息
    var GetQrCodeClientInfo ='//stageopenapi.vipabc.com/VMD/MGM.OfficalWebAPI/QRCode/GetQrCodeClientInfo';
  // 推荐有礼-----排行榜
    var APIGetClientPointRank = 'http://172.16.233.50:70/MemberPoint/api/ClientPoint/GetClientPointRank';
  // 推荐有礼-----我的推荐
  var APIGetClientPointSource = 'http://172.16.233.50:70/MemberPoint/api/ClientPoint/GetClientPointSource';
  // 推荐有礼-----我的推荐-----列表信息
  var APIGetPassivityNameList = 'http://172.16.233.50:70/MemberPoint/api/ClientPoint/GetPassivityNameList';
}
 else if (window.location.host.indexOf("stage") > -1) {
  var GetClientContractStatus ='//wx-member.tutorabc.com.cn/mgmplatform/MgmApi/GetClientContractStatus?';
  var GetQrCodeClientInfo ='//stageopenapi.vipabc.com/VMD/MGM.OfficalWebAPI/QRCode/GetQrCodeClientInfo';
  var APIGetClientPointRank = '//stageopenapi.vipabc.com/MemberPoint/api/ClientPoint/GetClientPointRank';
  var APIGetClientPointSource = '//stageopenapi.vipabc.com/MemberPoint/api/ClientPoint/GetClientPointSource';
  var APIGetPassivityNameList = '//stageopenapi.vipabc.com/MemberPoint/api/ClientPoint/GetPassivityNameList';
} 
else  {
  var GetClientContractStatus ='//web.tcnwc.weitutorstage.com/mgmplatform/MgmApi/GetClientContractStatus';
  var GetQrCodeClientInfo = "//openapi.vipabc.com/VMD/MGM.OfficalWebAPI/QRCode/GetQrCodeClientInfo?";
  var APIGetClientPointRank = '//openapi.vipabc.com/MemberPoint/api/ClientPoint/GetClientPointRank';
  var APIGetClientPointSource = '//openapi.vipabc.com/MemberPoint/api/ClientPoint/GetClientPointSource';
  var APIGetPassivityNameList = '//openapi.vipabc.com/MemberPoint/api/ClientPoint/GetPassivityNameList';

}
$(function () {
  //声明客户状态，成交超过30天还是未成交
   var status ;
   // 推荐有礼 客户状态的不同情况
  function giftOtherNone(){
    $(".no-buy").html("16000积分")

 }
 function giftOtherPass(){
    $(".no-buy").html("20000积分")
}

  //显示友福同享页面
  function friendShared() {
  $(".banner-box2").show();
  $(".banner-box1").hide();
  $(".s-text3").show();
  $(".s-text1").hide();
  $(".s-text2").hide();
  $(".s-item-share").show();
  $(".s-item-gift").hide();
  $(".free-name-list").html("查看观望团名单列表");
  //  $.ajax({
  //   type: "GET",
  //   cache: false,
  //   dataType:"json",
  //   url: tuijianoulijiekou,
  //   data: {
  //     'ClientSn': ClientSn
  //   },
  //   success:function(data) {     
  //   }
  // }); 
}
 //拿ClientSn  DayDiff
function getQrCodeClientInfo() {
  $.ajax({
    type: "GET",
    cache: false,
    // dataType:"json",
    url: GetQrCodeClientInfo,
    data: {
      'ClientSn': ClientSn
    },
    success: function(data) {
         console.log(data);
         DayDiff = data.DayDiff;
         clientName = data.Cname; 
    }

})
}

  //显示推荐有礼页面
  function recommendGift() {
    if(status  == 1){
      console.log("客户未成交");
      giftOtherNone()
   }
    else{
      console.log("成交超过30天");
       giftOtherPass()
      
    }      
  }

  // 新增代码
  var ClientSn = getQueryString('ClientSn');
  console.log(ClientSn);

  // var clientName = getQueryString('clientName');
  var clientName = "";

  var LoginInfo = getQueryString('LoginInfo');
  console.log(LoginInfo);

  var fromwhere = 'A0Tb6YCgfv';

  // var DayDiff = getQueryString('DayDiff');
  var DayDiff = "";
  $.ajax({
    type: "GET",
    cache: false,
    dataType:"json",
    url: GetClientContractStatus,
    data: {
      'ClientSn': ClientSn
    },
    success: function (data) {
      //获取客户clientsn daydiffy
       getQrCodeClientInfo()
        status = data.Status;
        console.log(data);
      //  status = 3;
       if(status ==2){
         //调用友福同享接口
          console.log("友福同享");
          friendShared();
        }
       else{
          //调用推荐有礼接口
           recommendGift();
       }

    }
  }); 
  

  if (!DayDiff) {
    DayDiff = 0;
  }
  if (!clientName) {
    clientName = '您';
  }
  if (parseInt(DayDiff) >= 0) {
   //shareTitle = clientName + '与tutorabc认识' + DayDiff + '天了，想把TA介绍给你！';
  //  增加代码
    shareTitle = clientName + '与tutorabc认识' + DayDiff + '天了，总是犹豫不决开始学英语？我们互相监督，开始行动吧！';
  } else {
    var dayx = parseInt(DayDiff) * (-1);
    shareTitle = clientName + '与外教练习口语已经' + dayx + '天，坚持的秘密就在这里！';
  }


  //console.log(shareTitle);
  shareLink = window.location.origin + '/program/linkage_page/mgm2016_event07/index.html?ClientSn=' + encodeURIComponent(ClientSn) + '&fromwhere=' + encodeURIComponent(fromwhere);
  /*if (ua_explorer.match(/MicroMessenger/i) == "micromessenger") {
      wxAPIConfig(shareLink);
  }*/

  $('#output').html('');
  $('#output').qrcode({
    render: "canvas", //设置渲染方式，有table和canvas，使用canvas方式渲染性能相对来说比较好
    text: shareLink, //扫描二维码后显示的内容,可以直接填一个网址，扫描二维码后自动跳向该链接
    width: 135, //二维码的宽度
    height: 135,
    background: "#ffffff", //二维码的后景色
    foreground: "#000000", //二维码的前景色
    src: 'http://source.vipabc.com/ext/images/website/abc/linkage_page/mgm2017_event10_16/logo.png',
  });

  var imgSrc = $('canvas')[0].toDataURL("image/png");
  document.getElementById('my_qrcode').src = imgSrc;

  $('.tab1').click(function () {
    $('.tab1').addClass('active');
    $('.tab2').removeClass('active');
    $('.s-box-tab1').show();
    $('.s-box-tab2').hide();
  });
  $('.tab2').click(function () {
    $('.tab1').removeClass('active');
    $('.tab2').addClass('active');
    $('.s-box-tab1').hide();
    $('.s-box-tab2').show();
    // 点击-排行版-让“显示更多”出来
    //$('.more-name2').show();

  });

  $('.more-name').click(function () {
    $(this).parent().find('.name-box').toggle();
  });
  $('.more-name2').click(function () {
    $('.more-name2').hide();
    $('.more-item').show();
  });

  $('.s-btn').click(function () {
     if (ua_explorer.match(/MicroMessenger/i) == "micromessenger") {
       $('.mask2').show();
     }
  });
  $('.mask2').click(function () {
    $('.mask2').hide();
  });

  $('.rule1').click(function () { 
    //推荐有礼活动详情 
    $('.mask1').show();
    
  });
  $('.rule3').click(function () {  
    // 友福同享活动详情
     $('.mask3').show();
  });

  $('.mask1-logo').click(function () {
    $('.mask1').hide();
    $('.mask3').hide();
  });
  

  $.ajax({
    type: "GET",
    cache: false,
    url: APIGetClientPointRank,
    data: {
      'MpID': LoginInfo
    },
    success: function (result) {
      console.log(result);
      if (result.ErrorCode == 200){
        var start_num = 0;
        if (result.Data.DataList.length < 11) {
          start_num = 0;
        } else {
          start_num = 1;
          var html1 = '<div class="s-item" style="border-bottom: 4px solid #efeeee;margin: 0 0 4px;"><div class="s-text5"><span class="s-text-red">第' + result.Data.DataList[0].ClientRank + '名</span>&nbsp;&nbsp;我</div><div class="s-text6">成功推荐<span class="s-text-red">' + result.Data.DataList[0].ClientCnt + '</span>位好友购买指定课程 </div></div>';
          $('.tab2-item-box').append(html1);
        }
  
        var html2 = '';
        for (var i = start_num; i < result.Data.DataList.length; i++) {
          if (i < 4) {
            html2 = ' <div class="s-item"><div class="s-text5">第' + result.Data.DataList[i].ClientRank + '名&nbsp;&nbsp;' + result.Data.DataList[i].ClientName.charAt(0) + '*</div><div class="s-text6">成功推荐<span class="s-text-red">' + result.Data.DataList[i].ClientCnt + '</span>位好友购买指定课程 </div></div>';
          } else {
            html2 = ' <div class="s-item more-item"><div class="s-text5">第' + result.Data.DataList[i].ClientRank + '名&nbsp;&nbsp;' + result.Data.DataList[i].ClientName.charAt(0) + '*</div><div class="s-text6">成功推荐<span class="s-text-red">' + result.Data.DataList[i].ClientCnt + '</span>位好友购买指定课程 </div></div>';
          }
          $('.tab2-item-box').append(html2);
        }
        
      }
       
    }

  });

  $.ajax({
    type: "GET",
    cache: false,
    url: APIGetClientPointSource,

    data: {
      'MpID': LoginInfo
    },
    success: function (result) {
      console.log(result);
      if (result.ErrorCode == 200) {
        if (result.Data.CourseCnt2 != 0) {
          // $('.friends-num').html(result.Data.CourseCnt2);
          // $('.nofroiends').hide();
          // $('.hasfriends').show();
        }
        $('.tab1-coursecnt1').html(result.Data.CourseCnt1);
        // $('.tab1-sumpoint').html(result.Data.SumPoint);
        $('.tab1-democnt').html(result.Data.DemoCnt);
        // $('.tab1-qrcodecnt').html(result.Data.QrCodeCnt);
      }

    }
  });

  $.ajax({
    type: "GET",
    cache: false,
    url: APIGetPassivityNameList,
    data: {
      'MpID': LoginInfo,
      'PageIndex': 1,
      'PageSize': 9999
    },
    success: function (result) {
      console.log("推荐列表",result);
      var arr_name = '';
      if (result.ErrorCode == 200) {
        // for (var i = 0; i < result.Data.PassivityName.length; i++) {
        //   if (i == 0) {
        //     arr_name = result.Data.PassivityName[i];
        //   } else {
        //     arr_name = arr_name + '，' + result.Data.PassivityName[i];
        //   }
        // }
        // if (arr_name != '') {
        //   $('.name').html(arr_name);
        // }
        for (var i = 0; i < result.Data.PassivityName.length; i++){
          if(result.Data.PassivityName.length != 0){
            $(".name").append(
            '<p>'+
            '<span class="user">陈*</span>'+
            '<span class="timer">2017年4月4日参与试听课程</span>'+
            '</p>'
          )
          }
        }



      }
    }
  });

});
